/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package requestManager;

import app.Global;
import app.MyModel;
import entities.EmployeeShiftRequest;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author yoshi
 */
public class RequestManagerModel implements MyModel {

    private long departmentId;
    private ArrayList<EmployeeShiftRequest> requests = new ArrayList<EmployeeShiftRequest>();
    
    public RequestManagerModel(long departmentId) {
        this.departmentId = departmentId;
    }
    
    public void initDefault(){
        Connection connection = Global.getConnection();
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            String q = Global.getRequestQuery()
                    + " WHERE ef.department_id = ? AND sf.date >= ? AND esr.request_date >= ? "
                    + " ORDER BY sf.date ASC, sf.time_slot_id ASC";

            pst = connection.prepareStatement(q);
            Date now = new Date(new java.util.Date().getTime());
            pst.setLong(1, departmentId);
            pst.setDate(2, now);
            pst.setDate(3, now);
            rs = pst.executeQuery();
            
            while (rs.next()) {
                requests.add(new EmployeeShiftRequest(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(RequestManagerDefault.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<EmployeeShiftRequest> getRequests() {
        return requests;
    }

    @Override
    public void clear() {
        requests.clear();
    }
    
}
